package ynu.edu.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        // 1. 创建 CORS 配置对象
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");  // 允许所有域
        config.setAllowCredentials(true);     // 是否发送 Cookie
        config.addAllowedMethod("*");         // 允许所有请求方法
        config.addAllowedHeader("*");         // 允许所有请求头部
        config.addExposedHeader("*");         // 暴露所有头部信息

        // 2. 添加地址映射
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);  // 映射到所有地址

        // 3. 返回 CorsFilter 对象
        return new CorsFilter(corsConfigurationSource);
    }
}
